package com.zf.generator.templates;

import com.zf.generator.bean.*;

public class ActionTemplate
{
  protected static String nl;
  public static synchronized ActionTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    ActionTemplate result = new ActionTemplate();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "package ";
  protected final String TEXT_2 = ";" + NL + "" + NL + "import java.text.SimpleDateFormat;" + NL + "import java.util.Date;" + NL + "import java.util.List;" + NL + "" + NL + "import javax.annotation.Resource;" + NL + "import javax.servlet.http.HttpServletRequest;" + NL + "" + NL + "import ";
  protected final String TEXT_3 = ".";
  protected final String TEXT_4 = ";" + NL + "import ";
  protected final String TEXT_5 = ".";
  protected final String TEXT_6 = ";" + NL + "import com.zf.generator.orm.PageData;" + NL + "import com.zf.generator.utils.WebUtils;" + NL + "import org.springframework.beans.propertyeditors.CustomDateEditor;" + NL + "import org.springframework.stereotype.Controller;" + NL + "import org.springframework.ui.Model;" + NL + "import org.springframework.ui.ModelMap;" + NL + "import org.springframework.web.bind.WebDataBinder;" + NL + "import org.springframework.web.bind.annotation.InitBinder;" + NL + "import org.springframework.web.bind.annotation.PathVariable;" + NL + "import org.springframework.web.bind.annotation.RequestMapping;" + NL + "import org.springframework.web.bind.annotation.RequestMethod;" + NL + "" + NL + "@Controller" + NL + "@RequestMapping(\"/";
  protected final String TEXT_7 = "/";
  protected final String TEXT_8 = "\")" + NL + "public class ";
  protected final String TEXT_9 = NL + "{" + NL + "\t@Resource" + NL + "\tprivate ";
  protected final String TEXT_10 = " ";
  protected final String TEXT_11 = ";" + NL + "" + NL + "\t@RequestMapping(\"/list\")" + NL + "\tpublic ModelMap list(HttpServletRequest request)" + NL + "\t{" + NL + "\t\tPageData<";
  protected final String TEXT_12 = "> pageData = new PageData<";
  protected final String TEXT_13 = ">();" + NL + "\t\t//给pageData设置参数" + NL + "\t\tWebUtils.setPageDataParameter(request, pageData);" + NL + "\t\tpageData = ";
  protected final String TEXT_14 = ".find(pageData);" + NL + "" + NL + "\t\treturn new ModelMap(pageData);" + NL + "\t}" + NL + "" + NL + "\t@RequestMapping(value = \"/new\")" + NL + "\tpublic String addForm(Model model)" + NL + "\t{" + NL + "\t\treturn \"/";
  protected final String TEXT_15 = "/";
  protected final String TEXT_16 = "/input\";" + NL + "\t}" + NL + "" + NL + "\t@RequestMapping(value = \"/edit/{id}\")" + NL + "\tpublic String editForm(@PathVariable(\"id\") Integer id, Model model)" + NL + "\t{" + NL + "\t\t";
  protected final String TEXT_17 = " ";
  protected final String TEXT_18 = " = ";
  protected final String TEXT_19 = ".find(id);" + NL + "\t\tmodel.addAttribute(";
  protected final String TEXT_20 = ");" + NL + "\t\treturn \"/";
  protected final String TEXT_21 = "/";
  protected final String TEXT_22 = "/input\";" + NL + "\t}" + NL + "" + NL + "\t@RequestMapping(value = \"/save\", method = RequestMethod.POST)" + NL + "\tpublic String save(";
  protected final String TEXT_23 = " ";
  protected final String TEXT_24 = ")" + NL + "\t{" + NL + "\t\tif (";
  protected final String TEXT_25 = ".isNew())" + NL + "\t\t{" + NL + "\t\t\t";
  protected final String TEXT_26 = ".save(";
  protected final String TEXT_27 = ");" + NL + "\t\t} else" + NL + "\t\t{" + NL + "\t\t\t";
  protected final String TEXT_28 = ".update(";
  protected final String TEXT_29 = ");" + NL + "\t\t}" + NL + "" + NL + "\t\treturn \"redirect:/";
  protected final String TEXT_30 = "/";
  protected final String TEXT_31 = "/list\";//重定向" + NL + "\t}" + NL + "" + NL + "\t@RequestMapping(value = \"/delete/{id}\")" + NL + "\tpublic String delete(@PathVariable(\"id\") Integer id)" + NL + "\t{" + NL + "\t\t";
  protected final String TEXT_32 = ".delete(id);" + NL + "\t\treturn \"redirect:/";
  protected final String TEXT_33 = "/";
  protected final String TEXT_34 = "/list\";//重定向" + NL + "\t}" + NL + "" + NL + "\t@RequestMapping(value = \"/visible/{id}\")" + NL + "\tpublic String visible(@PathVariable(\"id\") Integer id)" + NL + "\t{" + NL + "\t\t";
  protected final String TEXT_35 = ".visible(id);" + NL + "\t\treturn \"redirect:/";
  protected final String TEXT_36 = "/";
  protected final String TEXT_37 = "/list\";//重定向" + NL + "\t}" + NL + "" + NL + "\t@RequestMapping(value = \"/unVisible/{id}\")" + NL + "\tpublic String unVisible(@PathVariable(\"id\") Integer id)" + NL + "\t{" + NL + "\t\t";
  protected final String TEXT_38 = ".unVisible(id);" + NL + "\t\treturn \"redirect:/";
  protected final String TEXT_39 = "/";
  protected final String TEXT_40 = "/list\";//重定向" + NL + "\t}" + NL + "" + NL + "\t@InitBinder" + NL + "\tpublic void InitBinder(WebDataBinder dataBinder)" + NL + "\t{" + NL + "\t\tdataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat(\"yyyy-MM-dd\"), false));" + NL + "\t}" + NL + "" + NL + "}";
  protected final String TEXT_41 = NL;

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
	JavaTemplateArgs args = (JavaTemplateArgs)argument;
	
	String modeName = args.getModeName();
	String packageName = args.getPackageName();
	String className = args.getClassName();
	
	String entityPackageName = args.getPackageNameInfo().getEntityPackageName();
	String daoPackageName = args.getPackageNameInfo().getDaoPackageName();
	String servicePackageName = args.getPackageNameInfo().getServicPackageName();
	String actionPackageName = args.getPackageNameInfo().getActionPackageName();
	
	String entityClassName = args.getClassNameInfo().getEntityClassName();
	String daoClassName = args.getClassNameInfo().getDaoClassName();
	String serviceClassName = args.getClassNameInfo().getServicClassName();
	String actionClassName = args.getClassNameInfo().getActionClassName();
	
	String entityObjectName = args.getClassNameInfo().getEntityObjectName();
	String daoObjectName = args.getClassNameInfo().getDaoObjectName();
	String serviceObjectName = args.getClassNameInfo().getServicObjectName();
	String actionObjectName = args.getClassNameInfo().getActionObjectName();

    stringBuffer.append(TEXT_1);
    stringBuffer.append(packageName);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(entityPackageName);
    stringBuffer.append(TEXT_3);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_4);
    stringBuffer.append(servicePackageName);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(serviceClassName);
    stringBuffer.append(TEXT_6);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_8);
    stringBuffer.append(args.getClassName());
    stringBuffer.append(TEXT_9);
    stringBuffer.append(serviceClassName);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_14);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_15);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_16);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_17);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_18);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_19);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_20);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_21);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_22);
    stringBuffer.append(entityClassName);
    stringBuffer.append(TEXT_23);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_24);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_25);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_26);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_27);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_28);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_29);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_30);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_31);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_32);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_33);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_34);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_35);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_36);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_37);
    stringBuffer.append(serviceObjectName);
    stringBuffer.append(TEXT_38);
    stringBuffer.append(modeName);
    stringBuffer.append(TEXT_39);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_40);
    stringBuffer.append(TEXT_41);
    return stringBuffer.toString();
  }
}
